﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace calc
{
    public partial class Form1 : Form
    {

        [DllImport("user32.dll", EntryPoint = "FindWindow")]  //声明FindWindowAPI
        private extern static IntPtr FindWindow(string lpClassName, string lpWindowName);

        // Activate an application window.
        [DllImport("USER32.DLL")]
        public static extern bool SetForegroundWindow(IntPtr hWnd);
        
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            IntPtr hwnd = new IntPtr(0);
            hwnd = FindWindow(null, "计算器");//这里写的是计算器
            //判断这个窗体是否有效 
            if (hwnd == IntPtr.Zero)
            {
                MessageBox.Show("没有找到窗口", "系统提示");
                return;
            }
           
            // 然后使用SetForegroundWindow函数将这个窗口调到最前。
            SetForegroundWindow(hwnd);
            //发送按键
            SendKeys.SendWait("2");
            SendKeys.SendWait("*");
            SendKeys.SendWait("11");
            SendKeys.SendWait("{ENTER}");
        }
    }
}
